package xiaochangsama.the_age_of_immortals.procedures;

import xiaochangsama.the_age_of_immortals.TheAgeOfImmortalsModVariables;
import xiaochangsama.the_age_of_immortals.TheAgeOfImmortalsMod;

import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.IWorld;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.Entity;

import java.util.Map;
import java.util.HashMap;

public class HuiLingStartProcedure {
	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				TheAgeOfImmortalsMod.LOGGER.warn("Failed to load dependency entity for procedure HuiLingStart!");
			return;
		}
		if (dependencies.get("world") == null) {
			if (!dependencies.containsKey("world"))
				TheAgeOfImmortalsMod.LOGGER.warn("Failed to load dependency world for procedure HuiLingStart!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		IWorld world = (IWorld) dependencies.get("world");
		if (((entity.isPassenger()) && (((entity.getCapability(TheAgeOfImmortalsModVariables.PLAYER_VARIABLES_CAPABILITY, null)
				.orElse(new TheAgeOfImmortalsModVariables.PlayerVariables())).xiuLianZhuangTai) != 2))) {
			{
				double _setval = (double) 2;
				entity.getCapability(TheAgeOfImmortalsModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.xiuLianZhuangTai = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
			if (entity instanceof PlayerEntity)
				((PlayerEntity) entity).closeScreen();
		} else if (((entity.isPassenger()) && (((entity.getCapability(TheAgeOfImmortalsModVariables.PLAYER_VARIABLES_CAPABILITY, null)
				.orElse(new TheAgeOfImmortalsModVariables.PlayerVariables())).xiuLianZhuangTai) == 2))) {
			entity.getPersistentData().putString("xiuLianZhuangTai", "\u00A7l\u00A74\u4F60\u5DF2\u7ECF\u5728\u6062\u590D\u7075\u529B\u4E86");
			new Object() {
				private int ticks = 0;
				private float waitTicks;
				private IWorld world;
				public void start(IWorld world, int waitTicks) {
					this.waitTicks = waitTicks;
					MinecraftForge.EVENT_BUS.register(this);
					this.world = world;
				}

				@SubscribeEvent
				public void tick(TickEvent.ServerTickEvent event) {
					if (event.phase == TickEvent.Phase.END) {
						this.ticks += 1;
						if (this.ticks >= this.waitTicks)
							run();
					}
				}

				private void run() {
					{
						Map<String, Object> $_dependencies = new HashMap<>();
						$_dependencies.put("entity", entity);
						XiuLianZhuangTaiProcedure.executeProcedure($_dependencies);
					}
					MinecraftForge.EVENT_BUS.unregister(this);
				}
			}.start(world, (int) 20);
		} else {
			entity.getPersistentData().putString("xiuLianZhuangTai", "\u00A7l\u00A74\u6062\u590D\u7075\u529B\u9700\u8981\u5148\u5750\u4E0B");
			new Object() {
				private int ticks = 0;
				private float waitTicks;
				private IWorld world;
				public void start(IWorld world, int waitTicks) {
					this.waitTicks = waitTicks;
					MinecraftForge.EVENT_BUS.register(this);
					this.world = world;
				}

				@SubscribeEvent
				public void tick(TickEvent.ServerTickEvent event) {
					if (event.phase == TickEvent.Phase.END) {
						this.ticks += 1;
						if (this.ticks >= this.waitTicks)
							run();
					}
				}

				private void run() {
					{
						Map<String, Object> $_dependencies = new HashMap<>();
						$_dependencies.put("entity", entity);
						XiuLianZhuangTaiProcedure.executeProcedure($_dependencies);
					}
					MinecraftForge.EVENT_BUS.unregister(this);
				}
			}.start(world, (int) 20);
		}
	}
}
